﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.Model;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model.Enum;

public partial class Common_QueryDrugsByQualityLevel : BasePage
{

    public Common_QueryDrugsByQualityLevel()
    {
        this.ModuleNo = Module.药品统计分析.ToString();
        this.PageOperateType = OperateType.药品质量层次统计.ToString();
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        QueryControl1.OnQueryClick += new EventHandler(QueryControl1_OnQueryClick);
        if (!IsPostBack)
        {
            ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
            QueryControl1.BindDataPre((SuperviseQuery)ViewState["query"]);//绑定查询条件的一些数据源 质量层次等
            BindData();//绑定数据
            QueryControl1.BindSort(gvwInfo);
            GetQualitylevelsCount();
        }
    }

    void QueryControl1_OnQueryClick(object sender, EventArgs e)
    {
        this.AspNetPager1.CurrentPageIndex = 1;
        ViewState["query"] = QueryControl1.UpdateQueryFromValue((SuperviseQuery)ViewState["query"]);
        BindData();
        GetQualitylevelsCount();
    }

    private void BindData()
    {
        int t = 0;
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        ie.QueryObject = new HZ.Supervise.Model.QueryObject()
        {
            //采购目录编码汇总
            GroupField = GroupByField.QualityLevel,
            Query = (SuperviseQuery)ViewState["query"],
            GroupSelectField = GroupFields.PurchaseAmountTotal | GroupFields.DistributeAmountTotal | GroupFields.DistributeAmountTotal | GroupFields.DistributeRate | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseRate | GroupFields.DrugCount,
            SortField = QueryControl1.SortField == "" ? "QualityLevel ASC" : QueryControl1.SortField
        };

        System.Data.DataTable dt = ie.ExecutePage(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
        AspNetPager1.RecordCount = t;
        gvwInfo.PageSize = AspNetPager1.PageSize;
        GridViewUtil.AdjustGridView(gvwInfo, dt);
        this.gvwInfo.DataSource = dt;
        this.gvwInfo.DataBind();
        setChart(dt);
        SetColumnVisible();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }

    protected void gvQualityLevel_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //按企业查看
        if (e.CommandName == "DirectByCom")
        {
            //设置分组查询字段GroupSelectField
            if (ShowColumns.ShowColumn == null)
            {
                ShowColumns.ShowColumn = new ShowColumns();
                ShowColumns.ShowColumn.GroupFields = GroupFields.PurchaseCountTotal | GroupFields.PriceAvg | GroupFields.PurchaseAmountTotal | GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.WareHouseCountTotal | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseRate;
            }

            string sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            query.QualityLevel = e.CommandArgument.ToString();
            query._QueryPrompt = query._QueryPrompt + " " + e.CommandArgument.ToString();
            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings["CompanyID_PS"];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));

        }

        //按医院查看
        if (e.CommandName == "DirectByHospital")
        {
            //设置分组查询字段GroupSelectField
            if (ShowColumns.ShowColumn == null)
            {
                ShowColumns.ShowColumn = new ShowColumns();
                ShowColumns.ShowColumn.GroupFields = GroupFields.PurchaseCountTotal | GroupFields.PriceAvg | GroupFields.PurchaseAmountTotal | GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.WareHouseCountTotal | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseRate;
            }

            string sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            query.QualityLevel = e.CommandArgument.ToString();
            query._QueryPrompt = query._QueryPrompt + " " + e.CommandArgument.ToString();
            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings["HospitalID"];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }

        //按药品查看
        if (e.CommandName == "DierectByDrug")
        {
            //设置分组查询字段GroupSelectField
            if (ShowColumns.ShowColumn == null)
            {
                ShowColumns.ShowColumn = new ShowColumns();
                ShowColumns.ShowColumn.GroupFields = GroupFields.PurchaseCountTotal | GroupFields.PriceAvg | GroupFields.PurchaseAmountTotal | GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.WareHouseCountTotal | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseRate;
            }

            string sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            query.QualityLevel = e.CommandArgument.ToString();
            query._QueryPrompt = query._QueryPrompt + " " + e.CommandArgument.ToString();
            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings["ProcureCatalogID"];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }

        //查看详细
        if (e.CommandName == "DierectByDetail")
        {
            //设置分组查询字段GroupSelectField
            if (ShowColumns.ShowColumn == null)
            {
                ShowColumns.ShowColumn = new ShowColumns();
                ShowColumns.ShowColumn.GroupFields = GroupFields.PurchaseCountTotal | GroupFields.PriceAvg | GroupFields.PurchaseAmountTotal | GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.WareHouseCountTotal | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseRate;
            }

            string sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            query.QualityLevel = e.CommandArgument.ToString();
            query._QueryPrompt = query._QueryPrompt + " " + e.CommandArgument.ToString();
            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings["HosComDrugs"];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }
    }

    #region   数据准备
    /// <summary>

    /// 屏蔽相关查询连接
    /// </summary>
    private void SetColumnVisible()
    {
        if (!string.IsNullOrEmpty(QueryControl1.ProcureCatalogID))
        {
            foreach (GridViewRow row in gvwInfo.Rows)
            {
                row.FindControl("lbnDrug").Visible = false;
            }
        }
        if (!string.IsNullOrEmpty(QueryControl1.HospitalID))
        {
            foreach (GridViewRow row in gvwInfo.Rows)
            {
                row.FindControl("lbnHos").Visible = false;
            }
        }
        if (!string.IsNullOrEmpty(QueryControl1.CompanyID_PS))
        {
            foreach (GridViewRow row in gvwInfo.Rows)
            {
                row.FindControl("lbnComPS").Visible = false;
            }
        }
    }
    protected void ddlOther_SelectedIndexChanged(object sender, EventArgs e)
    {
        string stkey = "";
        string sessionid = string.Empty;
        GridViewRow row = (GridViewRow)((Control)sender).Parent.Parent;
        DropDownList ddl = (DropDownList)row.FindControl("ddlOther");
        stkey = ddl.SelectedValue;
        if (stkey != "")
        {
            sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            //QualityLevel,1根据页面情况定
            query.QualityLevel = row.Cells[1].Text; //((LinkButton)row.FindControl("lbnHos")).CommandArgument;
            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings[stkey];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }
    }

    #endregion


    /// <summary>
    /// 设置图表
    /// </summary>
    /// <param name="?"></param>
    private void setChart(System.Data.DataTable dt)
    {
        if (dt.Rows.Count > 0)
        {
            this.ChartControl1.Visible = false;
            HZ.Supervise.Web.WebService.ChartConfig serChart = new HZ.Supervise.Web.WebService.ChartConfig();
            dt.TableName = "质量层次";
            serChart.DataSource = dt;
            serChart.XName = "QualityLevel";
            serChart.YNames = "PurchaseAmountTotal";
            serChart.ChartTypes = HZ.Supervise.Web.WebService.ChartType.PieChart;

            ChartControl1.webChart = serChart;
            ChartControl1.EntranceChart();
        }
        else
        {
            ChartControl1.Visible = false;
        }
    }
    protected void lbtnSee_Click(object sender, EventArgs e)
    {
        if (this.lbtnSee.Text == "显示图表")
        {
            ChartControl1.Visible = true;
            this.lbtnSee.Text = "隐藏图表";
        }
        else
        {
            ChartControl1.Visible = false;
            this.lbtnSee.Text = "显示图表";
        }
    }

    protected void btnExcel_Click(object sender, EventArgs e)
    {
        int t = 0;
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        ie.QueryObject = new HZ.Supervise.Model.QueryObject()
        {
            //分组字段
            GroupField = GroupByField.QualityLevel,
            //查询条件
            Query = (SuperviseQuery)ViewState["query"],
            //选择分组字段
            GroupSelectField = GroupFields.PurchaseAmountTotal | GroupFields.DistributeAmountTotal | GroupFields.DistributeAmountTotal | GroupFields.DistributeRate | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseRate | GroupFields.DrugCount,
            //排序字段
            SortField = QueryControl1.SortField == "" ? "QualityLevel ASC" : QueryControl1.SortField
        };

        System.Data.DataTable dt = ie.ExecutePage(0, 0, out t);

        //GridViewUtil.AdjustGridView(gvwInfo, dt);

        ExportFile.Instance.ExportData(dt, gvwInfo, this.Page);
    }
    public void GetQualitylevelsCount()
    {
        int t = 0;
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        ie.QueryObject = new HZ.Supervise.Model.QueryObject()
        {
            //采购目录编码汇总
            GroupField = GroupByField.QualityLevel,
            Query = (SuperviseQuery)ViewState["query"],
            GroupSelectField = GroupFields.PurchaseAmountTotal | GroupFields.DistributeAmountTotal | GroupFields.DistributeAmountTotal | GroupFields.DistributeRate | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseRate | GroupFields.DrugCount,
            SortField = QueryControl1.SortField == "" ? "QualityLevel ASC" : QueryControl1.SortField
        };

        System.Data.DataTable dt = ie.ExecutePage(1, 0, out t);
        int DrugCount = 0;//品规
        double PurchaseAmountTotal = 0;//采购总金额
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DrugCount += HZ.MathUtil.SToI(dt.Rows[i]["DrugCount"].ToString());
                PurchaseAmountTotal += HZ.MathUtil.SToF(dt.Rows[i]["PurchaseAmountTotal"].ToString());
            }
            lblCount.Text = "品规总数为:" + DrugCount.ToString() + ";采购总金额为:" + PurchaseAmountTotal.ToString("F2") + "";
        }
        else
        {
            lblCount.Text = "暂无符合条件的数据！";
        }
    }


}
